// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_gray");
	var top = require("../top/top_main");
	var constant = require("constant");
	var databind = require("databind");
	var html_adjustTemporary = require("./html_adjustTemporary.html");
	var dialog = require("dialog");
	var css_enterpriseDetail = require("./enterpriseDetail.css");
	var flag = true;
	var wait = 60;
	var enterpriseId;
	var enterpriseName;
	var Eqianbao;
	//var mySignatureInfo;
	var promptTime;//e签宝预计审核完成时间
	var depositBank;
	var subbranch;
	var creditCard;
	var mobile;
	var auditState; 
	var tools = require("tools");
	var html_activeCard = require("./html_activeCard.html");
	var html_rebutReason = require("./html_rebutReason.html");
	var html_playMoney = require("./html_playMoney.html");
	var html_mobileInfo = require("./html_mobileInfo.html");
	var html_mobileCheckInfo = require("./html_mobileCheckInfo.html");
	var authentication; //认证状态
	var errorMessage; //认证驳回原因
	var enabled; //签章是否启用
	var imageSealPreview;
	var fileElementId;
	var fileUuid;
	var submitTime;
	var checkUuid;
	var submitDateTime;
	var auditDateTime;
	var modelType;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var oData = {
		pageIndex: pageIndex,
		pageSize: pageSize,
		totalPageCount: totalPageCount,
		totalRecordCount: totalRecordCount,
		statementBeginDate: "",
		statementEndDate: "",
		statementNumber: "",
		receiptName: "",
		minSourceAmount: "",
		maxSourceAmount: "",
		financeNumber: ""
	};
	var init = function(page) {
		if (page.params != null) {
			Eqianbao = page.params.Eqianbao; //显示实名认证标志
		}
		//mySignatureInfo = page.params.mySignature;
		inquireCurrentEnterpriseDetail();
		inquireUserCardInfoDetailFromEnterpriseId();
		router.init();
		initEvent();
	};

	var initEvent = function() {
			//点击法人印章
		$("#li3Id").unbind("click").click(function() {
			changeCssSealType($(this), 1);
			
			inquireSealDetail(1,null, function(data){
				
			});
		});
		
			//点击企业印章
		$("#li2Id").unbind("click").click(function() {
			changeCssSealType($(this), 2);
			
			inquireSealDetail(2,null, function(data){
				
			});
		});
		
			//点击财务印章
		$("#li4Id").unbind("click").click(function() {
			changeCssSealType($(this), 3);
			
			inquireSealDetail(3,null, function(data){
				
			});
		});
		
			//点击其他印章
		$("#li5Id").unbind("click").click(function() {
			changeCssSealType($(this), 4);
			
			inquireSealDetail(4,null, function(data){
				
			});
			
		});
		
		$(".container3 .next").unbind("click").click(function(){
			mobileCheckInfo(1);
			});
		
		//判断企业是否进行企业资质认证
		/*if(auditState == 2 || auditState == 4) { //显示实名认证
			$(".certification").removeClass("none");
		}*/
		//判断实名认证点击
		if(Eqianbao != undefined && Eqianbao == 1 ) {
			enterpriseDetailAuthentication(); 
		}
		
		//判断我的签章点击
		/*if(mySignatureInfo != undefined && mySignatureInfo == 1 ) {
			mySignatureCss();
		}*/
		//判断认证状态
		if(authentication != 3) { //隐藏我的签章
			$(".container3").hide();
		}
		if(authentication == 0 || authentication == undefined) { //未认证
			$("#span_stateInfo").text("未实名认证");
			$("#a_stateInfo").text("立即认证");
			$("#a_stateInfo").attr("href","../approve/approve_main.html");
		}else if(authentication == 5) { //认证驳回
			$("#span_stateInfo").text("实名被驳回");
			$("#a_stateInfo").text("查看驳回原因");
			$("#a_stateInfo").attr("href","javascript:void(0);");
			$("#a_stateInfo").attr("class","authenticationStateInfo");
		}else if(authentication == 2) { //已打款，输入审核金额
			$("#span_stateInfo").text("银行账号确认");
			$("#a_stateInfo").text("输入审核金额");
			$("#a_stateInfo").attr("href","javascript:void(0);");
			$("#a_stateInfo").attr("class","playMoneyStateInfo");
		}else if(authentication == 3) { //已认证
			$("#span_stateInfo").text("已认证");
			$("#a_stateInfo").text("查看认证信息情况");
			$("#a_stateInfo").attr("href","../approve/approve_main.html");
			$(".divImg1").children("img").attr("src","../../../res/png/rz_finish(1).png");
		}else if(authentication == 7) { //输入金额不正确，认证失败
			$("#span_stateInfo").text("银行账号确认错误");
			$("#a_stateInfo").text("查看错误原因");
			$("#a_stateInfo").attr("href","javascript:void(0);");
			$("#a_stateInfo").attr("class","failStateInfo");
		}else if(authentication == 1 || authentication == 6) { //实名待审核或打款中
			$("#span_stateInfo").text("实名待审核");
			$("#a_stateInfo").text("查看审核进度");
			$("#a_stateInfo").attr("href","javascript:void(0);");
			$("#a_stateInfo").attr("class","authenticationIng");
		}else if(authentication == 4) { //认证失败 
			$("#span_stateInfo").text("认证失败");
			$("#a_stateInfo").text("查看失败原因");
			$("#a_stateInfo").attr("href","javascript:void(0);");
			$("#a_stateInfo").attr("class","authenticationfailInfo");
		}
		
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}
		
		$('.bankInfo').card({
            container: $('.card-wrapper'),
            formSelectors: {
            	numberInput: "input[name=creditCard]",
            	nameInput: 'input[name="accountHolder"]',
			    expiryInput: 'input[name="expiry"]',
			    cvcInput: 'input[name="cvc"]'
            }
        });
		
		//查看认证驳回原因
		$(".authenticationStateInfo").unbind("click").click(function(){
			var ii = layer.open({
				  title: '驳回原因',
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['430px', '330px'], //宽高
				  content: html_rebutReason,
				  btn: ['重新认证'],
				  yes: function(){
					  location.href = "../approve/approve_main.html"; 
				  }
				});
			$("#reason").text(errorMessage);
			var text = '<span id="span1"><img src="../../../res/png/u438.png" /><span>';
			$(".layui-layer-title").prepend(text);
			$(".layui-layer-title").css({"text-align":"center","font-size":"30px","height":"60px","padding-top":"6px","color":"red"});
			$("#span1").css({"float":"left","margin-left":"60px"});
			$("#reason").css({"margin-top":"80px","font-size":"25px","margin-left":"100px"});
			$(".layui-layer-btn0").css({"position":"relative","margin-top":"-60px","margin-right":"140px","width":"140px","height":"40px","text-align":"center","padding-top":"6px","font-size":"18px","background-color":"rgb(231,64,49)"});
		});
		
		//查看认证失败原因
		$(".authenticationfailInfo").unbind("click").click(function(){
			var ii = layer.open({
				  title: '认证失败原因',
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['430px', '330px'], //宽高
				  content: html_rebutReason,
				  btn: ['重新认证'],
				  yes: function(){
					  location.href = "../approve/approve_main.html"; 
				  }
				});
			$("#reason").text(errorMessage);
			var text = '<span id="span1"><img src="../../../res/png/u438.png" /><span>';
			$(".layui-layer-title").prepend(text);
			$(".layui-layer-title").css({"text-align":"center","font-size":"30px","height":"60px","padding-top":"6px","color":"red"});
			$("#span1").css({"float":"left","margin-left":"60px"});
			$("#reason").css({"margin-top":"80px","font-size":"25px","margin-left":"100px"});
			$(".layui-layer-btn0").css({"position":"relative","margin-top":"-60px","margin-right":"140px","width":"140px","height":"40px","text-align":"center","padding-top":"6px","font-size":"18px","background-color":"rgb(231,64,49)"});
		});
		
		//实名待审核
		$(".authenticationIng").unbind("click").click(function(){
			var ii = layer.open({
				  title: '请耐心等待',
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['430px', '330px'], //宽高
				  content: html_rebutReason,
				  btn: ['关闭'],
				  yes: function(){
					  layer.close(ii); 
				  }
				});
			$("#reason").text("我司将在"+ promptTime +"左右完成审核，审核结果将通过短信或邮件的方式通知到您；审核通过后，会向贵公司的对公账号汇入一笔1元以下的金额，注意查收。注:(节假日申请，于最近一个工作日11:00到帐)");
			var text = '<span id="span1"><img src="../../../res/png/u309.png" /><span>';
			$(".layui-layer-title").prepend(text);
			$(".layui-layer-title").css({"text-align":"center","font-size":"30px","height":"60px","padding-top":"6px"});
			$("#span1").css({"float":"left","margin-left":"60px"});
			$("#reason").css({"margin-top":"px","font-size":"px","margin-left":"px","padding":"20px","margin-top":"30px"});
			$(".layui-layer-btn0").css({"position":"relative","margin-top":"-60px","margin-right":"140px","width":"140px","height":"40px","text-align":"center","padding-top":"6px","font-size":"18px","background-color":"rgb(196,62,62)"});
		});
		
		//查看金额输入失败原因
		$(".failStateInfo").unbind("click").click(function(){
			var ii = layer.open({
				  title: '金额确认错误原因',
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['430px', '330px'], //宽高
				  content: html_rebutReason,
				  btn: ['重新确认'],
				  yes: function(){
					  layer.close(ii);
					  playMoneyStateInfo();
				  }
				});
			$("#reason").text(errorMessage);
			var text = '<span id="span1"><img src="../../../res/png/u438.png" /><span>';
			$(".layui-layer-title").prepend(text);
			$(".layui-layer-title").css({"text-align":"center","font-size":"25px","height":"60px","padding-top":"6px","color":"red"});
			$("#span1").css({"float":"left","margin-left":"60px"});
			$("#reason").css({"margin-top":"80px","text-align":"center","font-size":"25px"});
			$(".layui-layer-btn0").css({"position":"relative","margin-top":"-60px","margin-right":"140px","width":"140px","height":"40px","text-align":"center","padding-top":"6px","font-size":"18px","background-color":"rgb(196,62,62)"});
		});
		
		//查看手机认证信息
		$(".mobileInfo").unbind("click").click(function(){
			var ii = layer.open({
				  title: '手机认证信息',
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['430px', '330px'], //宽高
				  content: html_mobileInfo,
				  btn: ['关闭'],
				  yes: function(){
					  layer.close(ii);
				  }
				});
			$("#mobileDiv").text(mobile);
			$(".layui-layer-title").css({"text-align":"center","font-size":"25px","height":"60px","padding-top":"6px"});
			$(".layui-layer-btn0").css({"position":"relative","margin-top":"-60px","margin-right":"140px","width":"140px","height":"40px","text-align":"center","padding-top":"6px","font-size":"18px"});
		});
		
		//输入审核金额
		$(".playMoneyStateInfo").unbind("click").click(function(){
			playMoneyStateInfo();
		});
		
		$("#enterpriseDetail").on("change", "#creditCard", function(){
			if ($.trim($(this).val()) == "") {
				return;
			}
			var bn = tools.getBankName($(this).val());
			if (bn == ""){
				base.alert(2, "未知开户行！");
				$("#depositBank").removeAttr("disabled");
				$("#depositBank").removeAttr("readonly");
			} else {
				$("#depositBank").val(bn);
				$("#depositBank").attr("disabled", "disabled");
				$("#depositBank").attr("readonly", "readonly");
			}
		});
		
		$(".activeCard").unbind("click").click(function(){
			$("#popdialog").html(html_activeCard);
			
			$("#activeCard_dialog .dialogTitle").html("开卡");
			
			$("#activeCard_dialog").on("change", "#cardFrontFile", function(){
				tools.uploadFile("cardFrontFile", function(data){
					$("label[for=cardFrontFile]").html(data.data[0].fileName);
					$(".cardFrontFile").val(data.data[0].fileUuid);
				});
			});
			
			$("#activeCard_dialog").on("change", "#cardBackFile", function(){
				tools.uploadFile("cardBackFile", function(data){
					$("label[for=cardBackFile]").html(data.data[0].fileName);
					$(".cardBackFile").val(data.data[0].fileUuid);
				});
			});
			
			dialog.showDialog($("#activeCard_dialog"));
			
			$("#activeCard_dialog .dialogHandlerBtn").unbind("click").click(function(){
				databind.submitDom($("#activeCard_dialog .dialogContent"), function(){
					base.alert(1, "提交成功！");
					dialog.hideDialog($("#activeCard_dialog"));
					router.route("account/enterpriseDetail.html");
				});
			});
		});

		$(".update").on("click", function() {
			$(".sure").removeClass("none");
			$(".update").addClass("none");
			$("#depositBank").removeAttr("readonly").removeAttr("disabled").removeClass("noInput1");
			$("#depositBank").parent().removeAttr("style");
			$("#accountHolder").removeAttr("readonly").removeAttr("disabled").removeClass("noInput1");
			$("#creditCard").removeAttr("readonly").removeAttr("disabled").removeClass("noInput1");
		});
		//提交企业基本信息
		$(".sure").on("click", function() {
			dialog.showConfirmDialog("提交企业信息", "确定要提交此企业信息吗", function() {
				databind.submitDom($(".enterpriseDetailContent"), function() {
					base.alert(1, "提交企业基本信息成功");
					router.route("account/enterpriseDetail.html");
				});
			});
		});
		
		//申请临时额度
		$("#enterpriseDetail .adjustTemporary").unbind("click").click(function(){
			$("#popdialog").html(html_adjustTemporary);
			databind.fillForm($("#temporary_dialog"), {
				enterpriseId: enterpriseId
			});
			
			$("#temporary_dialog").on("change", "#adjustTemporaryFile", function() {
//				uploadFile($("#clientManageAuditFile"), $("#temporary_dialog .addfile"), $(".evaluationFileUuid"), "clientManageAuditFile");
				tools.uploadFile("adjustTemporaryFile", function(data){
					var str = '<span class="showFiles"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">' + data.data[0].fileName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="removeFile">x</a></span>';
					$("#temporary_dialog .addfile").before(str);
					var fs = [];
					$.each($('#temporary_dialog .mas'), function() {
						fs.push($(this).attr("fileUuid"));
					});
					$("#temporary_dialog .evaluationFileUuid").val(fs);
				});
			});
			
			$(".footerBtn").on("click", "#sure", function() {
				databind.submitDom($("#temporary_dialog"), function() {
					base.alert(1, "资信等级调整成功！");
					dialog.hideDialog($("#temporary_dialog"));
				});
			});
			
			$("#temporary_dialog").on("click", ".removeFile", function() {
				$(this).parents(".showFiles").prev().remove();
				$(this).parents(".showFiles").remove();
				var fs = [];
				$.each($('#temporary_dialog .mas'), function() {
					fs.push($(this).attr("fileUuid"));
				});
				$("#temporary_dialog .evaluationFileUuid").val(fs);
			});
			
			dialog.showDialog($("#temporary_dialog"));
		});

		$("#enterpriseDetail").on("click", ".enterpriseDetailInfo", function() {
			$("#enterpriseDetail .d2").css("background", "url(../../../res/png/tab_bg1.png) center bottom no-repeat");
			$(".container1").addClass("none");
			$(".container2").addClass("none");
			$(".container").removeClass("none");
			$(".enterpriseDetailMaterial").css("color", "#000");
			$(".certification").css("color", "#000");
			$(this).css("color", "#ff7344");
		});
		
		$("#enterpriseDetail").on("click", ".enterpriseDetailMaterial", function() {
			$("#enterpriseDetail .d2").css("background", "url(../../../res/png/tab_bg2.png) center bottom no-repeat");
			$(".container").addClass("none");
			$(".container2").addClass("none");
			$(".container1").removeClass("none");
			$(".enterpriseDetailInfo").css("color", "#000");
			$(".certification").css("color", "#000");
			$(this).css("color", "#ff7344");
		});
		
		//实名认证
		$("#enterpriseDetail").on("click", ".certification", function() {
			enterpriseDetailAuthentication();
		});
		
		//我的签章
		/*$("#enterpriseDetail").on("click", ".mySignatureInfo", function() {
			mySignatureCss();
		});*/

		$("#enterpriseDetail").on("click", ".moneyReceiptStatement", function() {
			$("#enterpriseDetail .d2").css("background", "url(../../../res/png/tab_bg2.png) center bottom no-repeat");
			$(".container").addClass("none");
			$(".container1").removeClass("none");
			tools.loadList("inquireReceiptMoneyStatementPageList.json", $("#receiptManageBody"), $("#receiptManageTpl"), oData, function() {
				$(".detail").unbind("click").click(function() {
					//router.route("insuranceManage/insuranceDetail.html?insuranceId=" + $(this).parents("tr").attr("insuranceId") + "&financeId=" + $(this).parents("tr").attr("financeId"));
				});
			});
		});
		
		$("#enterpriseDetail").on("click", ".moneyDisbursementStatement", function() {
			$("#enterpriseDetail .d2").css("background", "url(../../../res/png/tab_bg3.png) center bottom no-repeat");
//			$(".container").addClass("none");
//			$(".container1").removeClass("none");
//			tools.loadList("inquireReceiptMoneyStatementPageList.json", $("#receiptManageBody"), $("#receiptManageTpl"), oData, function() {
//				$(".detail").unbind("click").click(function() {
//					//router.route("insuranceManage/insuranceDetail.html?insuranceId=" + $(this).parents("tr").attr("insuranceId") + "&financeId=" + $(this).parents("tr").attr("financeId"));
//				});
//			});
		});
		
		//改变模板类型
		$("#enterpriseDetail").on("change", "select[name='modelType']", function() {
			if($(this).val() == "1") { //模板印章样式
				changeCssModelType();
				//若为图片审核拒绝，再点击模板印章
				var sealType = $("#enterpriseDetail input[name='sealType']").val();
				var selectModelType = 1;//印章模板
				inquireSealDetail(sealType,selectModelType,function(data) {
					if(data.datas[0] == undefined) {
						previewEnterpriseTemplateSeal();
					}
				});
			}else if ($(this).val() == "2") { //图片印章样式
				$("#enterpriseDetail .templateSeal").css("display", "none");
				$("#enterpriseDetail .pictureSeal").css("display", "block");
				$("#enterpriseDetail span[type='template']").css("display", "none");
				$("#enterpriseDetail span[type='picture']").css("display", "inline");
				$("#enterpriseDetail .image-seal-preview").css("display", "none");
				$("#enterpriseDetail #fileUpload").css("display", "block");
				$("#enterpriseDetail .seal-submit").css("display", "block");
				
				//
				var sealType = $("#enterpriseDetail input[name='sealType']").val();
				var selectModelType = 2;//图片模板
				inquireSealDetail(sealType,selectModelType,function(data) {
					if(data.datas[0] == undefined) {
							
					}
				});
			}
		});
		
		//上传图片
		$("#enterpriseDetail").on("change", "input[name='file']", function() {
			fileElementId = $(this).attr("id");
			uploadMaterial(fileElementId);
		});
		
		//图片印章提交
		$("#enterpriseDetail").on("click", ".seal-submit", function() {
			//校验
			if(fileUuid == undefined || fileUuid == "") {
				base.alert(2, "请重新上传图片印章");
				return;
			}
			mobileCheckInfo(2);
		});
		
		//及时预览印章
		$("#enterpriseDetail").on("change", "input[name='hText']", function() {
			previewEnterpriseTemplateSeal();
		});
		
		$("#enterpriseDetail").on("change", "input[name='qText']", function() {
			previewEnterpriseTemplateSeal();
		});
	};
	
	//查询当前帐号企业明细
	var inquireCurrentEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireCurrentEnterpriseDetail.json",
			async: false,
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					auditState = data.datas[0].auditState;
					mobile = data.datas[0].mobile;
					depositBank = data.datas[0].depositBank;
					subbranch = data.datas[0].subbranch;
					creditCard = data.datas[0].creditCard;
					promptTime = data.datas[0].promptTime;
					authentication = data.datas[0].authentication;
					errorMessage = data.datas[0].errorMessage;
					enterpriseId = data.datas[0].enterpriseId;
					enterpriseName = data.datas[0].enterpriseName;
					databind.fillForm($("#enterpriseDetail"), data.datas[0]);
					
					$("#enterpriseDetail .data[name=enterpriseType]").html(constant.getValueByKey(76, data.datas[0].enterpriseType));//企业类型
					
					$("#enterpriseDetail .data[name=scardType]").html(constant.getValueByKey(77, data.datas[0].scardType));//证件类型
					
					$("#enterpriseDetail .data[name=legalArea]").html(constant.getValueByKey(78, data.datas[0].legalArea));//法定代表人归属地 
					
					$("#enterpriseDetail .data[name=unitTypeId]").html(constant.getValueByKey(79, data.datas[0].unitTypeId));//单位类型
					
					if(data.datas[0].scardType == 0) {
						$("#enterpriseDetail .data[name=codeUSC]").parent().html("");
					}else if(data.datas[0].scardType == "1") {
						$("#enterpriseDetail .data[name=certificate]").parent().parent().remove();
					}
					
					if(data.datas[0].sUserType == "以法定代表人身份注册") {
						$("#enterpriseDetail .data[name=procurator]").parent().parent().parent().remove();
					}
					
						if(data.datas[0].businessTimeLimit == "0") {
						$("#enterpriseDetail .data[name=businessTimeLimit]").html("长期");
					}
					
					$.each($(".materialContent>div"), function() {
						var $this = $(this);
						var str = "<a target='_blank' href='/finance/api/v1.0/downloadFile.file?fileUuid=" + $(this).html() + "'><img src='/finance/api/v1.0/downloadFile.file?fileUuid=" + $(this).html() + "'/></a>"
						$(this).html(str);
					});
					tools.barcode($("#enterpriseDetail .barcode"), data.datas[0].enterpriseCode, "code128");
					databind.disableInput($(".enterpriseDetailContent"));
					
					$("#enterpriseDetail .data[name=creditLine]").html(tools.formatCurrency($("#enterpriseDetail .data[name=creditLine]").html()));
					$("#enterpriseDetail .data[name=creditAlready]").html(tools.formatCurrency($("#enterpriseDetail .data[name=creditAlready]").html()));
					$("#enterpriseDetail .data[name=creditRemaining]").html(tools.formatCurrency($("#enterpriseDetail .data[name=creditRemaining]").html()));
					$("#enterpriseDetail input[name=registeredCapital]").val(tools.formatCurrency($("#enterpriseDetail input[name=registeredCapital]").val()));
					
					if (data.datas[0].authentication == 3) {
						$(".downloadPoiDoc").show();
					} else {
						$(".downloadPoiDoc").hide();
					}
					
					
					$(".downloadPoiDoc").unbind("click").click(function(){
//						$(this).attr("href", "/finance/api/v1.0/getPoiDoc.json?enterpriseId="+data.datas[0].enterpriseId);
//						window.open("/finance/api/v1.0/getPoiDoc.json?type=1&enterpriseId="+data.datas[0].enterpriseId);
//						window.open("/finance/api/v1.0/getPoiDoc.json?type=2&enterpriseId="+data.datas[0].enterpriseId);
//						window.open("/finance/api/v1.0/getPoiDoc.json?type=3&enterpriseId="+data.datas[0].enterpriseId);
//						window.open("/finance/api/v1.0/getPoiDoc.json?type=4&enterpriseId="+data.datas[0].enterpriseId);
//						window.open("/finance/api/v1.0/getPoiDoc.json?type=5&enterpriseId="+data.datas[0].enterpriseId);
//						window.open("/finance/api/v1.0/getPoiDoc.json?type=6&enterpriseId="+data.datas[0].enterpriseId);
						document.getElementById("ifile1").src="/finance/api/v1.0/getPoiDoc.json?enterpriseId="+data.datas[0].enterpriseId;
					});
					
				} else {
//					base.alert(3, data.message);
				}
			}
		});
	}
	
	//查询印章详情
	var inquireSealDetail = function(sealType,selectModelType,callback) {
		base.fnAjax({
			action: "inquireSealDetail.json",
			async: false,
			oData: {
				sealType: sealType,
				modelType: selectModelType
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					callback(data);
					if(data.datas[0] == undefined) {
						return;
					}
					enabled = data.datas.enabled;
					submitDateTime = data.datas[0].submitDateTime;
					auditDateTime = data.datas[0].auditDateTime;
					modelType = data.datas[0].modelType;
					
					if (data.datas[0].auditState == 0) { //1.未提交 -- 默认模板印章
						changeCssModelType();
						$(".sealSubmitInfo").css("display", "none");
						if (data.datas.length > 0) {
							if (data.datas[0].data != "") {
								$("#imgLook").attr("src", data.datas[0].sealSrc);
							} else {
								previewEnterpriseTemplateSeal();
							}
						}else {
						}
					} else if (data.datas[0].auditState == 1 || data.datas[0].auditState == 2 || data.datas[0].auditState == 4) { //2.已提交 未审核或审核通过或审核完成
						if(modelType == 1) { //2.1 模板印章
							changeCssSubTemplateSeal();
							$("#enterpriseDetail .next").hide();
							databind.disableInput($("#enterpriseDetail .container3"));
							if (data.datas.length > 0) {
								if (data.datas[0].data != "") {
									$("#imgLook").attr("src", data.datas[0].sealSrc);
								} else {
									$("#imgLook").attr("src", "../../../res/png/u750.png");
								}
							}else {
							}
							
							//显示印章提交审核信息
							changeCssSealSubmitInfo(enterpriseName,submitDateTime,auditDateTime,data);
						}else { //2.2图片印章
							changeCssSubPictureSeal();
							$("#enterpriseDetail #fileUpload").css("display", "none");
							$("#enterpriseDetail .seal-submit").css("display", "none");
							databind.disableInput($("#enterpriseDetail .container3"));
							if (data.datas.length > 0) {
								if (data.datas[0].data != "") {
									$("#imgLook1").attr("src", data.datas[0].sealSrc);
								} else {
									$("#imgLook1").attr("src", "../../../res/png/u750.png");
								}
							}else {
							}
							
							//显示印章提交审核信息
							changeCssSealSubmitInfo(enterpriseName,submitDateTime,auditDateTime,data);
						}
					} else if (data.datas[0].auditState == 3 || data.datas[0].auditState == 5) {// 3.已提交 审核拒绝或重新制章
						if(modelType == 1) { //3.1.1 模板印章
								changeCssSubTemplateSeal();
								$("#enterpriseDetail .next").show();
								databind.enableInput($("#enterpriseDetail .templateSeal"));
								if (data.datas.length > 0) {
									if (data.datas[0].data != "") {
										$("#imgLook").attr("src", data.datas[0].sealSrc);
									} else {
										$("#imgLook").attr("src", "../../../res/png/u750.png");
									}
								}else {
								}
								
								//显示印章提交审核信息
								changeCssSealSubmitInfo(enterpriseName,submitDateTime,auditDateTime,data);
							}else { //3.1.2图片印章
								changeCssSubPictureSeal();
								$("#enterpriseDetail #fileUpload").css("display", "block");
								$("#enterpriseDetail .seal-submit").css("display", "block");
								if (data.datas.length > 0) {
									if (data.datas[0].data != "") {
										$("#imgLook1").attr("src", data.datas[0].sealSrc);
									} else {
										$("#imgLook1").attr("src", "../../../res/png/u750.png");
									}
								}else {
								}
								
								//显示印章提交审核信息
								changeCssSealSubmitInfo(enterpriseName,submitDateTime,auditDateTime,data);
							}
							databind.enableInput($("#enterpriseDetail .typeP6"));
							databind.enableInput($("#enterpriseDetail .pictureSeal"));
							if(data.datas[0].auditState == 5) { //重新制章
								$(".sealSubmitInfo .sealAuditCallBackInfo").css("display", "block");
								inquirySignatureCallbackRecord(1);
							}
							if(data.datas[0].auditState == 3) { //拒绝信息
								$(".sealSubmitInfo .sealAuditRefuseInfo").css("display", "block");
								inquirySignatureCallbackRecord(2);
							}
				} else { //4. 其他
					base.alert(2, "印章获取失败");
				}
				$(".hText").val(data.datas[0].hText);
				$(".qText").val(data.datas[0].qText);
				$("#enterpriseDetail #auditState").html(constant.getValueByKey(18, data.datas[0].auditState));
			}
		}
		})
	}
	
	//查询用户卡详情
	var inquireUserCardInfoDetailFromEnterpriseId = function() {
		base.fnAjax({
			action: "inquireUserCardInfoDetailFromEnterpriseId.json",
			async: false,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#enterpriseDetail .cardInfo"), data.datas[0]);
				} else {
//					base.alert(3, data.message);
				}
			}
		});
	}
	
	//显示实名认证样式
	var enterpriseDetailAuthentication = function() {
		$("#enterpriseDetail .d2").css("background", "url(../../../res/png/tab_bg3.png) center bottom no-repeat");
		$(".container").addClass("none");
		$(".container1").addClass("none");
		$(".container2").removeClass("none");
		$(".enterpriseDetailInfo").css("color", "#000");
		$(".enterpriseDetailMaterial").css("color", "#000");
		//$(".mySignatureInfo").css("color", "#000");
		$(".certification").css("color", "#ff7344");
		inintSealDetail(); //初始化印章样式
	}
	
	/*//我的签章样式
	var mySignatureCss = function() {
		$("#enterpriseDetail .d2").css("background", "url(../../../res/png/tab_bg4.png) center bottom no-repeat");
		$(".container").addClass("none");
		$(".container1").addClass("none");
		$(".container2").addClass("none");
		$(".enterpriseDetailInfo").css("color", "#000");
		$(".enterpriseDetailMaterial").css("color", "#000");
		$(".certification").css("color", "#000");
		$(".mySignatureInfo").css("color", "#ff7344");
	}*/
	
	//输入审核金额
	var playMoneyStateInfo = function() {
			var ii = layer.open({
				  title: '企业对公账号确认',
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['430px', '330px'], //宽高
				  content: html_playMoney,
				  btn: ['确定'],
				  yes: function(){
					  var cash = $("#cash").val();
					  if(cash == 0 || cash == "") { //输入审核金额失败
						  base.alert(2, "输入金额不能为0或空!");
						  return;
					  }
					  databind.submitDom($(".inputCashInfo"), function(){
						  layer.close(ii);
					  	  history.go(0);
					  	});
				  }
				});
			$(".inputCashInfo #depositBank").val(depositBank);
			$(".inputCashInfo #subbranch").val(subbranch);
			$(".inputCashInfo #creditCard").val(creditCard);
			$(".layui-layer-title").css({"text-align":"center","font-size":"30px","height":"60px","padding-top":"6px","padding-left":"50px","color":"red"});
			$(".layui-layer-btn0").css({"position":"relative","margin-top":"-60px","margin-right":"140px","width":"140px","height":"40px","text-align":"center","padding-top":"6px","font-size":"18px","background-color":"rgb(196,62,62)"});
	
	}
	
	//初始化印章信息
	var inintSealDetail = function() {
		inquireSealDetail(2,null, function(data){
		
		});
	}
	
	//上传文件
	var uploadMaterial = function(fileElementId) {
		$.ajaxFileUpload({
			url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
			secureuri: false,
			fileElementId: fileElementId,
			dataType: 'json',
			async: true,
			success: function(data) {
				$("#enterpriseDetail .image-seal-preview").css("display", "block");
				$("#enterpriseDetail .divImg2").find("img").attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid="+ data.data[0].fileUuid);
				
				fileUuid = data.data[0].fileUuid;
			},
			complete: function(xml) {},
			error: function(data, status, e) {
				if (data.msg == null) {
					base.alert(2, "文件太大无法上传");
				} else {
					base.alert(2, data.msg);
				}
			}
		});
	};
	
	//提交
	var sealPictureSubmit = function() {
		var sealType = $("#enterpriseDetail input[name='sealType']").val();
		base.fnAjax({
			action: "sealPictureSubmit.json",
			async: false,
			oData: {
				fileUuid: fileUuid,
				sealType: sealType
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "图片印章提交成功");
					inquireSealDetail(sealType,null, function(data){
						
					});
				}else {
					if (data.message == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.message);
					}
				}
			}
		})
	};
	
	//切换css 未提交 模板印章样式
	var changeCssModelType = function() { 
		$("#enterpriseDetail select[name='modelType']").val(1);
		$("#enterpriseDetail .templateSeal").css("display", "block");
		$("#enterpriseDetail .pictureSeal").css("display", "none");
		$("#enterpriseDetail span[type='template']").css("display", "inline");
		$("#enterpriseDetail span[type='picture']").css("display", "none");
		$("#enterpriseDetail input[name='file']").val('');
		$("#enterpriseDetail .next").show();
		databind.enableInput($("#enterpriseDetail .container3"));
		databind.disableInput($("#enterpriseDetail .sealSubmitInfo"));
	}
	
	//切换css 已提交 显示提交信息
	var changeCssSealSubmitInfo = function(enterpriseName,submitDateTime,auditDateTime,data) { 
		$(".sealSubmitInfo").css("display", "block");
		$(".sealSubmitInfo .sealAuditCallBackInfo").css("display", "none");
		$(".sealSubmitInfo .sealAuditRefuseInfo").css("display", "none");
		$("#enterpriseDetail textarea[name='auditInfo']").text("");
		$(".sealSubmitInfo .enterpriseName").text(enterpriseName);
		$(".sealSubmitInfo .submitTime").text(submitDateTime);
		$(".sealSubmitInfo .auditTime").text(auditDateTime);
		$(".sealSubmitInfo .auditState1").html(constant.getValueByKey(18, data.datas[0].auditState));
		if(modelType == 1) {
			$(".sealSubmitInfo .modelType").text("模板印章");
		}else if (modelType == 2) {
			$(".sealSubmitInfo .modelType").text("图片印章");
		}
		databind.disableInput($("#enterpriseDetail .sealSubmitInfo"));
	}
	
	//已提交 模板印章样式
	var changeCssSubTemplateSeal = function() { 
		$("#enterpriseDetail select[name='modelType']").val(1);
		$("#enterpriseDetail .pictureSeal").css("display", "none");
		$("#enterpriseDetail .templateSeal").css("display", "block");
		$("#enterpriseDetail span[type='template']").css("display", "inline");
		$("#enterpriseDetail span[type='picture']").css("display", "none");
	}
	
	//已提交 图片印章样式
	var changeCssSubPictureSeal = function() { 
		$("#enterpriseDetail .templateSeal").css("display", "none");
		$("#enterpriseDetail .pictureSeal").css("display", "block");
		$("#enterpriseDetail span[type='template']").css("display", "none");
		$("#enterpriseDetail span[type='picture']").css("display", "inline");
		$("#enterpriseDetail .image-seal-preview").css("display", "block");
		$("#enterpriseDetail select[name='modelType']").val(2);
		$(".sealSubmitInfo").css("display", "block");
	}
	
	//印章类型样式
	var changeCssSealType = function(dom,sealType) {
		$("#enterpriseDetail input[name=sealType]").val(sealType);
		dom.addClass("liCss");
		dom.siblings().removeClass("liCss");
		if(sealType == 1) { //法人印章
			$(".typeP1").hide();
			$(".typeP2").hide();
			//改变印章大小
			$("#imgLook").css({"width":"129px","height":"129px"});
			$("#imgLook1").css({"width":"129px","height":"129px"});
			$(".divImg").css({"width":"160px","height":"160px"});
			$(".divImg2").css({"width":"160px","height":"160px"});
		}else {
			$(".typeP1").show();
			$(".typeP2").show();
			//改变印章大小
			$("#imgLook").css({"width":"159px","height":"159px"});
			$("#imgLook1").css({"width":"159px","height":"159px"});
			$(".divImg").css({"width":"180px","height":"180px"});
			$(".divImg2").css({"width":"180px","height":"180px"});
		}
		
		//清上传文件
		fileUuid = undefined;
	}
	
	//印章提交信息审核
	var sealSubmitCheckInfo = function() {
		databind.submitDom($(".container3"), function(data) {
				if(data.status == 200) {
					var sealType = $("#enterpriseDetail input[name='sealType']").val();
					inquireSealDetail(sealType,null, function(data){
						
					});
				}else {
					base.alert(2, data.message);
				}
			});
	}
	
	//获取验证码
	var receiveCode = function(m) {
		base.fnAjax({
			action: "sealSubmitCheckInfo.json",
			nocode: true,
			oData: {
				mobile: m
			}, //传递参数
			fnSuccess: function(data) {
				flag = true;
				if (data.status == 200) {
					base.alert(1, "验证码发送成功！");
					time();
					$(".mobileCheckInfo input[name='checkUuid']").val(data.datas[0].checkUuid);
					checkUuid = data.datas[0].checkCode;
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	//限制60秒内不能重复获取验证码
	var time = function() {
		if (wait == 0) {
			$("#sendCode").removeAttr("disabled");
			$("#sendCode").html("获取验证码");
			wait = 60;
		} else {
			$("#sendCode").unbind("click");
			$("#sendCode").attr("disabled", "true");
			$("#sendCode").html("重新发送(" + wait + ")");
			wait--;
			setTimeout(function() {
				time()
			},
			1000);
		}
	}
	
	//及时生成印章
	var previewEnterpriseTemplateSeal = function() {
		var sealType = $("#enterpriseDetail input[name='sealType']").val();
		var hText = $("#enterpriseDetail input[name='hText']").val();
		var qText = $("#enterpriseDetail input[name='qText']").val();
		//校验
		if(hText.length > 8) {
			base.alert(2, "横向文不能超过8个汉字长度");
			return;
		}
		if(qText.length > 20) {
			base.alert(2, "下弦文过长");
			return;
		}
		
		base.fnAjax({
			action: "previewEnterpriseTemplateSeal.json",
			nocode: true,
			oData: {
				hText: hText,
				qText: qText,
				sealType: sealType,
				remark: ""
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$(".divImg").children("img").attr("src", data.datas[0].sealData);
				} else {
					base.alert(2, "印章预览失败");
				}
			}
		});
	};
	
	var mobileCheckInfo = function(modelType) {
		//获取当前日期
		var myDate = new Date();
	　　　var year = myDate.getFullYear();
	　　　if(year<10){
	　　　　　year="0"+year;
	　　　}
	　　　var month = myDate.getMonth()+1;
	　　　if(month<10){
	　　　　　month="0"+month;
	　　　}
	　　　var day = myDate.getDate();
	　　　if(day<10){
	　　　　　day="0"+day;
	　　　}
		submitTime = year + "-" + month + "-" + day;
		
		var ii = layer.open({
			  title: '印章提交审核信息',
			  type: 1,
			  skin: 'layui-layer-rim', //加上边框
			  area: ['430px', '330px'], //宽高
			  content: html_mobileCheckInfo,
			  btn: ['提交'],
			  yes: function(){
			  	var code = $(".mobileCheckInfo input[name='authenticode']").val();
			  	var checkUuid= $(".mobileCheckInfo input[name='checkUuid']").val();
			  	
			  	if(!$(".mobileCheckInfo input[type='checkbox']").prop("checked")) {
			  		base.alert(2, "请勾选承诺");
			  	} else if (code == "" || code == null) {
			  		base.alert(2, "请输入验证码");
			  	}else if (code.length != 6) {
					base.alert(2, "请输入正确的验证码！");
				} else if (checkUuid == "" || checkUuid == null) {
			  		base.alert(2, "验证码获取不正确");
			  	} else {
		  			databind.submitDom($(".mobileCheckInfo"), function(data) {
						if(data.status == 200) {
							layer.close(ii);
							wait = 0;
							if(modelType == 1) {
					  			sealSubmitCheckInfo();
					  		}else if (modelType == 2) {
					  			sealPictureSubmit();
					  		}
						}else {
							base.alert(2, data.message);
						}
					});
			  	}
			  }
			})
		$(".mobileCheckInfo .submitTime").text(submitTime);
		$(".mobileCheckInfo .enterpriseName").text(enterpriseName);
		$(".mobileCheckInfo input[name='enterpriseName']").val(enterpriseName);
		$(".mobileCheckInfo input[name='mobile']").val(mobile);
		
		//发送验证码
		$(".mobileCheckInfo").on("click", "#sendCode", function() {
			if(!$(".mobileCheckInfo input[type='checkbox']").prop("checked")) {
			  	base.alert(2, "请勾选承诺");
			  	return;	
			}
			if (flag && wait == 60) {
			flag = false;
			receiveCode($(".mobileCheckInfo input[name=mobile]").val());
			}
			if (wait > 0) {
				$("#sendCode").unbind("click");
			}
		})
	}

	//查询重新制章记录
	var inquirySignatureCallbackRecord = function(recordType) {
		
		base.fnAjax({
			action: "inquirySignatureCallbackRecord.json",
			async: true,
			nocode: true,
			oData: {
				enterpriseId: enterpriseId,
				sealType: $("#enterpriseDetail input[name='sealType']").val(),
				recordType: recordType
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if(data.datas[0] != undefined) {
						$("#enterpriseDetail textarea[name='auditInfo']").text(data.datas[0].remark);
					}
				}else {
					//
				}
			}
		})
	}
	
	return {
		init: init
	};
});